﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; 

namespace WindowsGame1.gamePlugins.menu
{
    class Button
    {
        Texture2D image, pressedImage;
        SpriteFont font;
        Rectangle location;
        string text = "";
        Vector2 textLocation;
        SpriteBatch spriteBatch;
        MouseState mouse;
        MouseState oldMouse;
        public bool clicked = false;

        public Button(Texture2D texture, SpriteFont font, SpriteBatch sBatch)
        {
            image = texture;
            this.font = font;
            location = new Rectangle(0, 0, image.Width, image.Height);
            spriteBatch = sBatch;
        }

        public void pressedTexture(Texture2D texture) {
            pressedImage = texture;
        }

        public string Text
        {
            get { return text; }
            set
            {
                text = value;
                Vector2 size = font.MeasureString(text);
                textLocation = new Vector2();
                textLocation.Y = location.Y + ((image.Height / 2) - (size.Y / 2));
                textLocation.X = location.X + ((image.Width / 2) - (size.X / 2));
            }
        }

        public void Location(int x, int y)
        {
            location.X = x;
            location.Y = y;
        }

        public void Update()
        {
            mouse = Mouse.GetState();
            clicked = false;
            if (mouse.LeftButton == ButtonState.Released && oldMouse.LeftButton == ButtonState.Pressed)
            {
                if (location.Contains(new Point(mouse.X, mouse.Y)))
                {
                    clicked = true;
                }
            }

            Text = "";
            oldMouse = mouse;
        }

        public void Draw()
        {
            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);

            if (location.Contains(new Point(mouse.X, mouse.Y)))
            {
                if (Mouse.GetState().LeftButton == ButtonState.Pressed && pressedImage != null)
                {
                   spriteBatch.Draw(pressedImage,
                            location,
                            Color.White);
                }
                else
                {
                    spriteBatch.Draw(image,
                            location,
                            Color.Silver);
                }
            }
            else
            {
                spriteBatch.Draw(image,
                    location,
                    Color.White);
            }

            if (clicked)
            {
                Vector2 position = new Vector2(10, 75);
            }

            spriteBatch.End();
        }
    }
}
